package com.example.jussi.threadsocketdemo;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.io.IOException;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {
    TextView textView;
    EditText content, ip;
    ClientThread clientThread;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == 0x01){
                textView.append("\n" + msg.obj.toString());
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        content = (EditText) findViewById(R.id.editText);
        ip = (EditText) findViewById(R.id.editText2);
        textView = (TextView) findViewById(R.id.textView2);
        clientThread = new ClientThread(handler, "192.168.1.106");
        new Thread(clientThread).start();
    }

    public  void change(View hhView){
        clientThread = new ClientThread(handler, ip.getText().toString());
        new Thread(clientThread).start();
    }

    public void send(View hhView) {
        Message message = new Message();
        message.what = 0x02;
        message.obj = content.getText().toString();
        try {
            clientThread.revHandler.sendMessage(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
